package com.samge.fandroid

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlin.concurrent.thread

open class BaseActivity : AppCompatActivity() {

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        Log.i(this.localClassName, "onNewIntent")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i(this.localClassName, "onCreate")
    }

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        Log.i(this.localClassName, "onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)")
//        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
        Log.i(this.localClassName, "onStart")
    }

    override fun onRestart() {
        super.onRestart()
        Log.i(this.localClassName, "onRestart")
    }

    override fun onResume() {
        super.onResume()
        Log.i(this.localClassName, "onResume")

        testCarshOnUiThread()
        testCarshOnThread()
    }

    override fun onPause() {
        super.onPause()
        Log.i(this.localClassName, "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.i(this.localClassName, "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(this.localClassName, "onDestroy")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.i(this.localClassName, "onSaveInstanceState ")
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        Log.i(this.localClassName, "onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) ")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.i(this.localClassName, "onRestoreInstanceState")
    }

    override fun onRestoreInstanceState(
        savedInstanceState: Bundle?,
        persistentState: PersistableBundle?
    ) {
        super.onRestoreInstanceState(savedInstanceState, persistentState)
        Log.i(this.localClassName, "onRestoreInstanceState(savedInstanceState: Bundle?,persistentState: PersistableBundle?)")
    }

    override fun onPostResume() {
        super.onPostResume()
        Log.i(this.localClassName, "onPostResume")
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        Log.i(this.localClassName, "onPostCreate")
    }

    override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onPostCreate(savedInstanceState, persistentState)
        Log.i(this.localClassName, "onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)")
    }

    /**
     * 崩溃测试
     * 主线程中崩溃：不走onDestroy()
     * 子线程中崩溃：走onDestroy()
     * 任务栏中直接杀掉app：只有MainActivity走onDestroy()，其他activity只会走onPause() onSaveInstanceState() onStop()
     */
    private fun testCarshOnUiThread(){
        findViewById<TextView>(R.id.btn_carsh)?.setOnClickListener {
            print(1/0)
        }
    }

    /**
     * 崩溃测试
     * 主线程中崩溃：不走onDestroy()
     * 子线程中崩溃：不走onDestroy()，会走onPause()
     * 任务栏中直接杀掉app：只有MainActivity走onDestroy()，其他activity只会走onPause() onSaveInstanceState() onStop()
     */
    private fun testCarshOnThread(){
        findViewById<TextView>(R.id.btn_carsh_thread)?.setOnClickListener {
            thread {
                print(1/0)
            }
        }
    }
}